<?php

class Student_GradeController extends Zend_Controller_Action
{
    protected $the_assess_id;
    public function init()
    {
    }

    public function indexAction()
    {
        $this->view->title = "My Grades";
       $this->view->headTitle($this->view->title, 'PREPEND');

       $grades = new Student_Model_DbTable_Grade();
       $assess_id = $this->_getParam('id');
       $allGrades = $grades->getSingleUserGrades($assess_id);
       $temp = 0;
       foreach($allGrades as $aG){
           $temp = $aG->id_of_assessment;
           //echo $temp;
       }
       $course = new Student_Model_DbTable_Assessment();
       $course_id = $course->getAssessDetails($temp);
       $id = 0;
       foreach($course_id as $ci){
            $id = $ci->id_of_course;
       }
       $this->view->id = $id;
       $this->view->grades = $allGrades;

    }

    public function addAction()
    {
        $assess_id = $this->_getParam('id');
        $assess = new Student_Model_DbTable_Assessment();

        $num = $assess->getAssessDetails($assess_id);

        $total_assign = 0;
        foreach ($num as $n){
               $total_assign = $n->num_of_assign;
        }
        $total_assign_disp =  "(There are $total_assign assessments in this group)";

       $this->view->title = "<b>Add Grades</b> ";
       $this->view->disp = $total_assign_disp;
       $this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Student_Form_newGrade();
        $this->view->form = $form;

        //gets the course id
        $this->view->assess_id = $assess_id;

        if ($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData))
                        {                       
    			$grade = $form->getValue('grade');
                        $max_grade_in_percent = $form->getValue('max_grade_in_percent');
                       // echo $max_grade_in_percent;
                        $grade_in_percent = round((($grade/100)*$max_grade_in_percent),2);
                        //$grade_in_percent = 5;
                      	$add = new Student_Model_DbTable_Grade();
    			$add->addGrade($assess_id, $grade, $grade_in_percent, $max_grade_in_percent );
                        
    			$this->view->message = "<u> Following grade was added:</u><br><br>";

                        $this->view->id = $assess_id;
                        $this->view->grade = '<i>Grade:'.$grade.', ';
                        $this->view->percent = '<i>Grade in Percent:</i> '.$grade_in_percent.'%'.', ';
                        $this->view->max = '<i>Max Weight:</i> '.$max_grade_in_percent.'%'.'<br><br></i>';
                        
                                             

                        }
                else
                    {
    			$form->populate($formData);
                    }
    }
    }



    public function editAction()
    {
        $this->view->title = "Edit Grade";
        $this->view->headTitle($this->view->title);
        $form = new Student_Form_NewGrade();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        if ($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                        $id = $this->_getParam('id');
                       // echo $id;
                        $grade = $form->getValue('grade');
                        $max_grade_in_percent=$form->getValue('max_grade_in_percent');
                        $grade_in_percent = round((($grade/100)*$max_grade_in_percent),2);
                        $edit = new Student_Model_DbTable_Grade();


                        $valid = $form->getValues();
                        print_r($valid);
                        
                        $edit->editGrade($id, $grade, $grade_in_percent, $max_grade_in_percent);
                        $this->_redirect('/student/grade/index/id/'.$id);
                }
                else
                {
                    $form->populate($formData);
                }
        }
        else
        {
            $id = $this->_getParam('id');
            if ($id > 0)
            {
                $edit = new Student_Model_DbTable_Grade();
                $form->populate($edit->getGrade($id));
            }
        }

    }

    public function deleteAction()
    {
        $this->view->title = "Delete Grade";
        $this->view->headTitle($this->view->title, 'PREPEND');
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $course = new Student_Model_DbTable_Grade();
                $course->deleteGrade($id);
            }
            $this->_redirect('/student/grade/index/id/');
            } else {

                //
             $id = $this->_getParam('id', 0);
             $delete = new Student_Model_DbTable_Grade();
            $this->view->grade = $delete->getGrade($id);
}
    }

    public function addmoreAction(){
        $this->view->title = "Add another Grade";
        $this->view->headTitle($this->view->title, 'PREPEND');

        if ($this->getRequest()->isPost()) {
            $adds = $this->getRequest()->getPost('add');
                        if ($adds == 'Yes') {
                        $id = $this->_getParam('id');
                        $this->_redirect('/student/grade/add/');
                        echo $id;

                        //$gr = new Student_Model_DbTable_Grade();
                        // echo $id;
                       // $gr->addGrade($assess_id, $grade, $grade_in_percent, $max_grade_in_percent);
            }
            if($adds == 'No'){
                $id = $this->_getParam('id');
                echo $id;
                $this->_redirect('/student/grade/index/id/'.$this->the_assess_id);
            
            }
            
            } else {

                //
             $id = $this->_getParam('id');
             $adds = new Student_Model_DbTable_Grade();
            //$this->view->grade = $adds->getGrade($assess_id);
    }
    



}
}

